let http = require('http')
let querystring = require('querystring')

let postData = querystring.stringify({
  'msg': 'hello node.js'
})

let options = {
  hostname: 'localhost',
  port: 3000,
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
}

let req = http.request(options, (res) => {
  console.log('STATUS:'+ res.statusCode)
  console.log('HEADERS:' + JSON.stringify(res.headers))
  res.setEncoding('utf8')

  // get data as chunks
  res.on('data', (chunk) => {
    console.log('BODY:' + chunk)
  })

  // end response
  res.on('end' , () => {
    console.log('NO more data in response.')
  })
})

req.on('error', (e) => {
  console.log('Problem with request: ' + e.message)
})

// write data to request body
req.write(postData)
req.end()

console.log('Server running....')